/*
 * main.c
 *
 *  Created on: 2015年7月18日
 *      Author: tao
 */

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include "../driver/eforce.h"


int main(int argc, char *argv[])
{
	int fd; //设备文件
	const char *filename = "/dev/eforce";

	__u32 dir0, dir1; //方向
	__u32 pwmval0, pwmval1; //pwm值
	__u32 brake;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		printf("open failed\n");
		return -1;
	}
	dir0 = 0;
	dir1 = 0;
	pwmval0 = 0;
	pwmval1 = 0;
	brake = 0;
	ioctl(fd, EFORCE_BRAKE, &brake);
	while (1) {

		ioctl(fd, EFORCE_SET_DIR0, &dir0);
		ioctl(fd, EFORCE_SET_DIR1, &dir1);

		ioctl(fd, EFORCE_SET_PWM0, &pwmval0);

		ioctl(fd, EFORCE_SET_PWM1, &pwmval1);

		dir0++;
		dir0 = dir0 & 1;

		dir1++;
		dir1 = dir1 & 1;

		pwmval0 += EFORCE_PWM_MAX / 5 - 1;
		if (pwmval0 >= EFORCE_PWM_MAX / 2) {
			pwmval0 = 0;
		}

		pwmval1 += EFORCE_PWM_MAX / 5 - 1;
		if (pwmval1 >= EFORCE_PWM_MAX / 2) {
			pwmval1 = 0;
		}

		sleep(1);

	}
	return 0;
}
